using System;
using System.Collections.Generic;
using System.Text;

namespace Wrath.Xml.Objects
{
    public class Size : IElementContainer
    {
        private int width = 0;

        public int Width
        {
            get { return width; }
            set { width = value; }
        }

        private int height = 0;

        public int Height
        {
            get { return height; }
            set { height = value; }
        }

        public Size(Element element)
        {
            sourceElement = element;

            Element subElement = element.FindFirstElement("width", false);
            if (subElement != null)
            {
                try
                {
                    width = Convert.ToInt32(subElement.Value);
                }
                catch
                {
                    width = 0;
                }
            }

            subElement = element.FindFirstElement("height", false);
            if (subElement != null)
            {
                try
                {
                    height = Convert.ToInt32(subElement.Value);
                }
                catch
                {
                    height = 0;
                }
            }
        }

        #region IElementContainer Members

        private Element sourceElement;
        public Element SourceElement
        {
            get { return sourceElement; }
        }

        public void Commit()
        {
            sourceElement = new Element("size");

            if (width != 0)
            {
                Element widthElement = new Element("width", width.ToString());
                sourceElement.ChildElements.Add(widthElement);
            }

            if (height != 0)
            {
                Element heightElement = new Element("height", height.ToString());
                sourceElement.ChildElements.Add(heightElement);
            }
        }

        #endregion
    }
}
